package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.TDailyStatement;
import com.ruoyi.domain.TDailyStatementAdmin;
import com.ruoyi.mapper.TDailyStatementMapper;
import com.ruoyi.pojo.dto.SubDailyStatementDto;
import com.ruoyi.service.ITDailyStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 每日汇报Service业务层处理
 * 
 * @author mzr
 * @date 2025-04-11
 */
@Service
public class TDailyStatementServiceImpl implements ITDailyStatementService
{
    @Autowired
    private TDailyStatementMapper tDailyStatementMapper;
    @Autowired
    private TDailyStatementAdminServiceImpl statementAdminService;

    /**
     * 查询每日汇报
     * 
     * @param id 每日汇报主键
     * @return 每日汇报
     */
    @Override
    public TDailyStatement selectTDailyStatementById(Long id)
    {
        return tDailyStatementMapper.selectTDailyStatementById(id);
    }

    /**
     * 查询每日汇报列表
     * 
     * @param tDailyStatement 每日汇报
     * @return 每日汇报
     */
    @Override
    public List<TDailyStatement> selectTDailyStatementList(TDailyStatement tDailyStatement)
    {
        return tDailyStatementMapper.selectTDailyStatementList(tDailyStatement);
    }

    /**
     * 新增每日汇报
     * 
     * @param tDailyStatement 每日汇报
     * @return 结果
     */
    @Override
    public int insertTDailyStatement(TDailyStatement tDailyStatement)
    {
        return tDailyStatementMapper.insertTDailyStatement(tDailyStatement);
    }

    /**
     * 修改每日汇报
     * 
     * @param tDailyStatement 每日汇报
     * @return 结果
     */
    @Override
    public int updateTDailyStatement(TDailyStatement tDailyStatement)
    {
        return tDailyStatementMapper.updateTDailyStatement(tDailyStatement);
    }

    /**
     * 批量删除每日汇报
     * 
     * @param ids 需要删除的每日汇报主键
     * @return 结果
     */
    @Override
    public int deleteTDailyStatementByIds(Long[] ids)
    {
        return tDailyStatementMapper.deleteTDailyStatementByIds(ids);
    }

    /**
     * 删除每日汇报信息
     * 
     * @param id 每日汇报主键
     * @return 结果
     */
    @Override
    public int deleteTDailyStatementById(Long id)
    {
        return tDailyStatementMapper.deleteTDailyStatementById(id);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED ,rollbackFor = Exception.class)
    public AjaxResult subDailyStatement(SubDailyStatementDto body, Long userId) {
        //根据userId和时间查询当日是否已经提交过日报 - 提交过就异常返回  - 没提交则发起提交
        Date date = new Date();
        int userIdI = Integer.parseInt(String.valueOf(userId));
        TDailyStatement dbDailyStatement = tDailyStatementMapper.loadByUserIdAndDate(date,userIdI);
        if (dbDailyStatement!=null){
            return AjaxResult.error("今日已提交过，请勿重复提交！");
        }
        TDailyStatement dailyStatement = new TDailyStatement();
        dailyStatement.setCompleted(body.getCompleted());
        dailyStatement.setUncompleted(body.getUncompleted());
        dailyStatement.setNeedCoordination(body.getNeedCoordination());
        dailyStatement.setTheAmountSigned(body.getTheAmountSigned());
        dailyStatement.setTomorrowPlan(body.getTomorrowPlan());
        dailyStatement.setGroupChatName(body.getGroupChatName());
        dailyStatement.setStaffId(userIdI);
        this.insertTDailyStatement(dailyStatement);
        Long id = dailyStatement.getId();
        System.out.println("拿到自增id"+id);
        for (Integer adminId : body.getAdminIds()) {
            TDailyStatementAdmin statementAdmin = new TDailyStatementAdmin();
            statementAdmin.setAdminId(adminId);
            statementAdmin.setDailyStatementId(id);
            statementAdminService.insertTDailyStatementAdmin(statementAdmin);
        }
        return AjaxResult.success();
    }


}
